package com.aiju.cash.ui.multiple_shops.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.bean.Shop;
import com.aiju.cash.R;
import com.aiju.cash.core.api.ShopManagetApi;
import com.aiju.cash.core.event.LimitsEvent;
import com.aiju.common.event.EventCenter;
import com.aiju.utils.T;

/**
 * @author: 佐为
 * @date: 2017-06-13 13:41
 * @version: V1.0
 * @Description: 配置店铺权限
 */
public class ShopLimitsActivity extends BaseTitleActivity {

    private Shop mShop;

    private String modifyRelationGoods, deleteRelationGoods, manageOwnGoods;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_limits);
        setCenterTitle("配置店铺权限");

        mShop = (Shop) getIntent().getSerializableExtra("shop");
        modifyRelationGoods = mShop.getModifyRelationGoods();
        deleteRelationGoods = mShop.getDeleteRelationGoods();
        manageOwnGoods = mShop.getManageOwnGoods();
        if (TextUtils.isEmpty(modifyRelationGoods)) {
            modifyRelationGoods = "0";
        }
        if (TextUtils.isEmpty(deleteRelationGoods)) {
            deleteRelationGoods = "0";
        }
        if (TextUtils.isEmpty(manageOwnGoods)) {
            manageOwnGoods = "0";
        }
        findViewById(R.id.tv_limits_manager).setSelected(manageOwnGoods.equals("1") ? true : false);
        findViewById(R.id.tv_limits_delete).setSelected(deleteRelationGoods.equals("1") ? true : false);
        findViewById(R.id.tv_limits_update).setSelected(modifyRelationGoods.equals("1") ? true : false);

        findViewById(R.id.tv_limits_manager).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());
                manageOwnGoods = v.isSelected() ? "1" : "0";
                updateShopGoodsAuth();
            }
        });
        findViewById(R.id.tv_limits_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());
                deleteRelationGoods = v.isSelected() ? "1" : "0";
                updateShopGoodsAuth();
            }
        });
        findViewById(R.id.tv_limits_update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setSelected(!v.isSelected());
                modifyRelationGoods = v.isSelected() ? "1" : "0";
                updateShopGoodsAuth();
            }
        });

    }

    /**
     * 店铺权限配置接口
     */
    private void updateShopGoodsAuth() {
        new ShopManagetApi(mDialogHelper).updateShopGoodsAuth(mShop.getStoreId(), modifyRelationGoods,
                deleteRelationGoods, manageOwnGoods, new SimpleHttpCallback() {
                    @Override
                    protected void onDataSuccess(String data) {
                        EventCenter.getInstance().post(new LimitsEvent());
                        T.ToastShort("权限设置成功");
                    }
                });
    }
}
